#pragma once

#include <atomic>
#include <utility>

#include "ShmMessageNode.hpp"




class ShmSharedPtrNode
{
public:
    // 带参数的构造函数
    ShmSharedPtrNode(const ShmMemoryBlock& myBlock, const ShmMemoryBlock& manageBlock)
        : myblock(myBlock)
        , manageBlock(manageBlock)
        , _count(0) {}

    // 拷贝构造函数
    ShmSharedPtrNode(const ShmSharedPtrNode& other)
        : myblock(other.myblock)
        , manageBlock(other.manageBlock)
        , _count(other._count.load(std::memory_order_relaxed)) {}

    // 拷贝赋值运算符
    ShmSharedPtrNode& operator=(const ShmSharedPtrNode& other) {
        if (this != &other) {
            myblock = other.myblock;
            manageBlock = other.manageBlock;
            _count.store(other._count.load(std::memory_order_relaxed), std::memory_order_relaxed);
        }
        return *this;
    }

    // 移动构造函数
    ShmSharedPtrNode(ShmSharedPtrNode&& other) noexcept
        : myblock(std::exchange(other.myblock, { -1, -1, -1 })),
        manageBlock(std::exchange(other.manageBlock, { -1, -1, -1 })),
        _count(other._count.load(std::memory_order_relaxed)) {}

    // 移动赋值运算符
    ShmSharedPtrNode& operator=(ShmSharedPtrNode&& other) noexcept {
        if (this != &other) {
            myblock = std::exchange(other.myblock, { -1, -1, -1 });
            manageBlock = std::exchange(other.manageBlock, { -1, -1, -1 });
            _count.store(other._count.load(std::memory_order_relaxed), std::memory_order_relaxed);
        }
        return *this;
    }

    // 析构函数
    ~ShmSharedPtrNode() = default;

    // 成员变量
    ShmMemoryBlock myblock;
    ShmMemoryBlock manageBlock;
    std::atomic<uint16_t> _count; // 引用计数
};